package locations.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import locations.dao.DAOFactory;
import locations.dao.InstitutionDAO;
import locations.dao.InstitutionResponsibleDAO;
import locations.dao.InstitutionTypeDAO;
import locations.model.Institution;
import locations.model.InstitutionResponsible;
import locations.model.InstitutionType;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class EditInstitutionAction extends ActionSupport implements
		ModelDriven<Institution>, ServletRequestAware {

	private static final long serialVersionUID = -8124967730231198191L;

	private HttpServletRequest request;
	private Institution institution = new Institution();
	private List<InstitutionResponsible> institutionResponsibles = new ArrayList<InstitutionResponsible>();
	private List<InstitutionType> institutionTypes = new ArrayList<InstitutionType>();

	public String execute() {

		InstitutionResponsibleDAO dao = DAOFactory
				.getInstitutionResponsibleDAO();
		this.setInstitutionResponsibles(dao.retrieveAll());

		InstitutionTypeDAO dao2 = DAOFactory.getInstitutionTypeDAO();
		this.setInstitutionTypes(dao2.retrieveAll());

		Long id = Long.parseLong(this.getRequest().getParameter("id"));
		InstitutionDAO dao3 = DAOFactory.getInstitutionDAO();
		this.institution = dao3.retrieveById(id);
		return SUCCESS;
	}

	public List<InstitutionResponsible> getInstitutionResponsibles() {
		return institutionResponsibles;
	}

	public void setInstitutionResponsibles(
			List<InstitutionResponsible> institutionResponsibles) {
		this.institutionResponsibles = institutionResponsibles;
	}

	public List<InstitutionType> getInstitutionTypes() {
		return institutionTypes;
	}

	public void setInstitutionTypes(List<InstitutionType> institutionTypes) {
		this.institutionTypes = institutionTypes;
	}

	@Override
	public Institution getModel() {
		return this.institution;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	public Institution getInstitution() {
		return institution;
	}

	public void setInstitution(Institution institution) {
		this.institution = institution;
	}

}
